<template>
  <div class="app">
    <header class="header">
      <div class="container">
        <div class="logo">
          <router-link to="/">数字藏品论坛</router-link>
        </div>
        <div class="mobile-menu-btn" @click="toggleMobileMenu">
          <span class="bar"></span>
          <span class="bar"></span>
          <span class="bar"></span>
        </div>
        <nav class="nav" :class="{ 'mobile-nav-active': mobileMenuOpen }">
          <router-link to="/" class="nav-link" @click="closeMobileMenu">首页</router-link>
          <router-link to="/ranking" class="nav-link" @click="closeMobileMenu">排行榜</router-link>
          <router-link to="/forum" class="nav-link" @click="closeMobileMenu">论坛</router-link>
        </nav>
        <div class="user-actions" :class="{ 'mobile-user-active': mobileMenuOpen }">
          <template v-if="userStore.isLoggedIn">
            <router-link to="/user/profile" class="user-profile" @click="closeMobileMenu">
              {{ userStore.userInfo.username || '用户' }}
            </router-link>
            <button @click="handleLogout" class="logout-btn">退出</button>
          </template>
          <template v-else>
            <router-link to="/user/login" class="login-btn" @click="closeMobileMenu">登录</router-link>
          </template>
        </div>
      </div>
    </header>

    <main class="main">
      <router-view />
    </main>

    <footer class="tabbar">
      <router-link to="/" class="tabbar-item" active-class="active">
        <Icon name="home" class="tabbar-icon" />
        <span class="tabbar-text">首页</span>
      </router-link>
      <router-link to="/ranking" class="tabbar-item" active-class="active">
        <Icon name="ranking" class="tabbar-icon" />
        <span class="tabbar-text">排行榜</span>
      </router-link>
      <router-link to="/forum" class="tabbar-item" active-class="active">
        <Icon name="forum" class="tabbar-icon" />
        <span class="tabbar-text">论坛</span>
      </router-link>
      <router-link to="/user/profile" class="tabbar-item" active-class="active">
        <Icon name="user" class="tabbar-icon" />
        <span class="tabbar-text">我的</span>
      </router-link>
    </footer>
  </div>
</template>

<script setup>
import { useUserStore } from '@/stores/user'
import { useRouter } from 'vue-router'
import { ref } from 'vue'
import Icon from '@/components/Icon.vue'

const userStore = useUserStore()
const router = useRouter()
const mobileMenuOpen = ref(false)

const toggleMobileMenu = () => {
  mobileMenuOpen.value = !mobileMenuOpen.value
}

const closeMobileMenu = () => {
  mobileMenuOpen.value = false
}

const handleLogout = () => {
  userStore.logout()
  router.push('/')
  closeMobileMenu()
}
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'Arial', sans-serif;
  line-height: 1.6;
  color: #333;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4edf9 100%);
  min-height: 100vh;
}

.container {
  width: 100%;
  padding: 0 20px;
}

.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4edf9 100%);
}

.header {
  background: linear-gradient(135deg, #ffffff 0%, #e0f2fe 100%);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}

.header .container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 70px;
}

.logo a {
  font-size: 1.5rem;
  font-weight: bold;
  background: linear-gradient(135deg, #3b82f6 0%, #0ea5e9 100%);
  background-clip: text;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  text-decoration: none;
}

.nav {
  display: flex;
  gap: 20px;
}

.nav-link {
  color: #334155;
  text-decoration: none;
  font-weight: 500;
  transition: all 0.3s ease;
  position: relative;
}

.nav-link::after {
  content: '';
  position: absolute;
  bottom: -5px;
  left: 0;
  width: 0;
  height: 2px;
  background: linear-gradient(135deg, #3b82f6 0%, #0ea5e9 100%);
  transition: width 0.3s ease;
}

.nav-link:hover,
.nav-link.router-link-active {
  color: #3b82f6;
}

.nav-link:hover::after,
.nav-link.router-link-active::after {
  width: 100%;
}

.user-actions {
  display: flex;
  align-items: center;
  gap: 15px;
}

.user-profile {
  color: #334155;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s ease;
}

.user-profile:hover {
  color: #3b82f6;
}

.login-btn,
.register-btn {
  padding: 8px 16px;
  border-radius: 8px;
  text-decoration: none;
  font-weight: 500;
  transition: all 0.3s ease;
}

.login-btn {
  color: #3b82f6;
  border: 1px solid #3b82f6;
}

.login-btn:hover {
  background: rgba(59, 130, 246, 0.1);
}

.register-btn {
  background: linear-gradient(135deg, #3b82f6 0%, #0ea5e9 100%);
  color: white;
  box-shadow: 0 4px 10px rgba(59, 130, 246, 0.3);
}

.register-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 15px rgba(59, 130, 246, 0.4);
}

.logout-btn {
  background: none;
  border: none;
  color: #64748b;
  cursor: pointer;
  font-weight: 500;
  transition: color 0.3s ease;
}

.logout-btn:hover {
  color: #ef4444;
}

.main {
  flex: 1;
  padding-bottom: 70px;
  /* 为底部tabbar留出空间 */
}

.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: linear-gradient(135deg, #ffffff 0%, #e0f2fe 100%);
  box-shadow: 0 -4px 15px rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: space-around;
  padding: 8px 0;
  z-index: 100;
  backdrop-filter: blur(10px);
  border-top: 1px solid rgba(255, 255, 255, 0.2);
}

.tabbar-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #64748b;
  text-decoration: none;
  transition: all 0.3s ease;
  padding: 5px 0;
  width: 25%;
}

.tabbar-icon {
  font-size: 24px;
  margin-bottom: 4px;
  color: inherit;
  display: flex;
  justify-content: center;
  align-items: center;
}

.tabbar-text {
  font-size: 12px;
  font-weight: 500;
}

.tabbar-item.active {
  color: #3b82f6;
}

.tabbar-item:hover {
  color: #3b82f6;
}

/* 确保图标在所有状态下都可见 */
.tabbar-item .icon {
  color: inherit !important;
}

/* 移动端菜单按钮 */
.mobile-menu-btn {
  display: none;
  flex-direction: column;
  justify-content: space-between;
  width: 30px;
  height: 21px;
  cursor: pointer;
}

.bar {
  height: 3px;
  width: 100%;
  background: linear-gradient(135deg, #3b82f6 0%, #0ea5e9 100%);
  border-radius: 3px;
  transition: all 0.3s ease;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header .container {
    height: 60px;
  }

  .nav,
  .user-actions {
    display: none;
  }

  .mobile-menu-btn {
    display: flex;
  }

  .nav.mobile-nav-active,
  .user-actions.mobile-user-active {
    display: flex;
    flex-direction: column;
    position: absolute;
    top: 60px;
    left: 0;
    right: 0;
    background: linear-gradient(135deg, #ffffff 0%, #e0f2fe 100%);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
    padding: 15px;
    gap: 15px;
  }

  .nav.mobile-nav-active {
    top: 60px;
  }

  .user-actions.mobile-user-active {
    top: 160px;
  }

  .main {
    padding-bottom: 60px;
  }
}

@media (max-width: 480px) {
  .container {
    padding: 0 15px;
  }

  .logo a {
    font-size: 1.2rem;
  }
}
</style>
